package com.david.func_test.view

import android.content.Context
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import com.david.func_test.R

class AvatarsView: View {
    private lateinit var bitmap:Bitmap
    private var coverBitmap:Bitmap
    val paint = Paint()

    private val bitmaps = ArrayList<Bitmap>()

    init {
        paint.strokeWidth = dp2px(1f)
        paint.style = Paint.Style.STROKE
    }

    constructor(context: Context, attr: AttributeSet): super(context, attr) {
        coverBitmap = getBitmap(context, R.mipmap.avater_cover)
        loadBitmaps()

        coverBitmapRect = Rect(0, 0, coverBitmap.width, coverBitmap.height)
        bitmapRect = Rect(0, 0, bitmap.width, bitmap.height)
        dstRect = Rect(0, 0, bitmap.width, bitmap.height)
    }

    private var coverBitmapRect:Rect
    private var bitmapRect:Rect
    private var dstRect:Rect

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        for ((index, item) in bitmaps.withIndex()) {

            dstRect.set(dp2px(index * 20f).toInt(), dp2px(1f).toInt(), dp2px(index * 20 + 24f).toInt(), dp2px(25f).toInt())
            canvas.drawBitmap(item, bitmapRect, dstRect, paint)
            if (index == bitmaps.size - 1) {
                canvas.drawBitmap(coverBitmap,
                    coverBitmapRect,
                    dstRect,
                    paint)
            }
            if (index == 0) {
                paint.color = Color.RED

            } else {
                paint.color = Color.WHITE
            }
            canvas.drawCircle(dp2px(index * 20f + 12f), dp2px(13f), dp2px(12f), paint)
        }
//        canvas?.drawBitmap(bitmap, Rect(0, 0, bitmap.width, bitmap.height), Rect(0, dp2px(1f).toInt(), dp2px(24f).toInt(), dp2px(25f).toInt()), paint)
//        paint.color = Color.RED
//        canvas?.drawCircle(dp2px(12f), dp2px(13f), dp2px(12f), paint)
//        canvas?.drawBitmap(bitmap, Rect(0, 0, bitmap.width, bitmap.height), Rect(dp2px(20f).toInt(), dp2px(1f).toInt(), dp2px(44f).toInt(), dp2px(25f).toInt()), paint)
//        paint.strokeWidth = dp2px(1f)
//        paint.style = Paint.Style.STROKE
//        paint.color = Color.WHITE
//        canvas?.drawCircle(dp2px(32f), dp2px(13f), dp2px(12f), paint)
    }

    private fun dp2px(dp: Float): Float {
        val r = Resources.getSystem()
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.displayMetrics)
    }

    private fun loadBitmaps() {
        bitmap = getBitmap(context, R.mipmap.avatar)
        bitmaps.add(bitmap)
        bitmaps.add(bitmap)
        bitmaps.add(bitmap)
        bitmaps.add(bitmap)
        bitmaps.add(bitmap)
    }

    private fun getBitmap(context: Context, resId: Int): Bitmap {
        val options = BitmapFactory.Options()
        val value = TypedValue()
        context.resources.openRawResource(resId, value);
        options.inTargetDensity = value.density;
        options.inScaled=false;//不缩放
        return BitmapFactory.decodeResource(context.resources, resId, options);
    }
}